package com.binance.notification.verifier;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.binance.master.validator.internal.constraintvalidators.iv.IVerifier;
import com.binance.notification.api.push.IMessage;
import com.binance.notification.api.push.notification.AndroidPayload;
import com.binance.notification.api.push.notification.AppPayload;
import com.binance.notification.api.push.notification.IosPayload;
import com.binance.notification.api.push.notification.PcPayload;
import com.binance.notification.api.push.notification.WebPayload;

@Component
public class PusMessageVerifier implements IVerifier<IMessage> {

    @Value("${jpush.android.alert.maxSize:100}")
    private int andoridAlertSize;
    @Value("${jpush.android.title.maxSize:32}")
    private int androidTitleSize;
    @Value("${jpush.ios.alert.maxSize:999999}")
    private int iosAlertSize;
    @Value("${jpush.ios.title.maxSize:999999}")
    private int iosTitleSize;

    @Override
    public boolean valid(IMessage value, ValidResult result) {
        if (value instanceof AppPayload) {
            this.validAppMessage((AppPayload) value, result);
        } else if (value instanceof PcPayload) {
            this.validPcMessage((PcPayload) value, result);
        } else if (value instanceof WebPayload) {
            this.validWebMessage((WebPayload) value, result);
        }
        return result.isEmpty();
    }

    private void validAppMessage(AppPayload message, ValidResult result) {
        if (message instanceof AndroidPayload) {
            if (message.getTitle().length() > this.androidTitleSize) {
                result.addError("Android设备推送标题不能超过" + this.androidTitleSize + "个字符长度！");
            }
            if (message.getContent().length() > this.andoridAlertSize) {
                result.addError("Android设备推送内容不能超过" + this.andoridAlertSize + "个字符长度！");
            }
        }
        if (message instanceof IosPayload) {
            if (message.getTitle().length() > this.iosTitleSize) {
                result.addError("IOS设备推送标题不能超过" + this.iosTitleSize + "个字符长度！");
            }
            if (message.getContent().length() > this.iosAlertSize) {
                result.addError("IOS设备推送内容不能超过" + this.iosAlertSize + "个字符长度！");
            }
        }
        if (message.getAction() == 1 && StringUtils.isBlank(message.getIntent())) {
            result.addError("当action属性为1时，intent属性不能为空！");
        }
        if (message.getAction() == 2 && StringUtils.isBlank(message.getWeb())) {
            result.addError("当action属性为2时，web属性不能为空！");
        }
    }

    private void validPcMessage(PcPayload message, ValidResult result) {
        if (message.getAction() == 0) {
            return;
        }
        if (message.getAction() == 1 && StringUtils.isBlank(message.getWeb())) {
            result.addError("当action属性为1时，web属性不能为空！");
        }
        if (message.getAction() == 2 && StringUtils.isBlank(message.getIntent())) {
            result.addError("当action属性为2时，intent属性不能为空！");
        }
    }

    private void validWebMessage(WebPayload message, ValidResult result) {
        if (message.getAction() == 0) {
            return;
        }
        if (message.getAction() == 1 && StringUtils.isAllBlank(message.getWap(), message.getWeb())) {
            result.addError("当action属性为1时，web和wap属性不能同时为空！");
        }
    }

}
